package org.sylfra.idea.plugins.xstructure.settings;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserFactory;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;

/**
 * Settings panel for xStructure shown in general settings dialog
 * <p/>
 * UI made with IDEA UI Designer
 *
 * @author <a href="mailto:sylfradev@yahoo.fr">Sylvain FRANCOIS</a>
 * @version $Id: XStructureSettingsPane.java 49 2009-04-09 19:33:43Z syllant $
 */
public class XStructureSettingsPane implements Disposable
{
  private JTextField tfMappingsStorageDir;
  private JCheckBox cbSyncMappings;
  private JPanel contentPane;
  private JCheckBox cbOverwriteWhenSyncMappings;

  public XStructureSettingsPane()
  {
    $$$setupUI$$$();

    // Disable cbOverwriteWhenSyncMappings if cbSyncMappings is not checked
    cbSyncMappings.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        cbOverwriteWhenSyncMappings.setEnabled(cbSyncMappings.isSelected());
      }
    });
  }

  /**
   * Returns the content pane
   *
   * @return the content pane
   */
  public JPanel getContentPane()
  {
    return contentPane;
  }

  /**
   * Update UI from settings bean (auto-generated)
   *
   * @param data the settings bean
   */
  public void setData(XStructureSettings data)
  {
    cbSyncMappings.setSelected(data.isSyncMappingsAtStartup());
    tfMappingsStorageDir.setText(data.getMappingsStorageDir());
    cbOverwriteWhenSyncMappings.setSelected(data.isOverwriteWhenSyncMappings());
  }

  /**
   * Update settings bean from UI (auto-generated)
   *
   * @param data the settings bean to update
   */
  public void getData(XStructureSettings data)
  {
    data.setSyncMappingsAtStartup(cbSyncMappings.isSelected());
    data.setMappingsStorageDir(tfMappingsStorageDir.getText());
    data.setOverwriteWhenSyncMappings(cbOverwriteWhenSyncMappings.isSelected());
  }

  /**
   * Returns true if the settings have been modified, false otherwise (auto-generated)
   *
   * @param data the settings bean
   * @return true if the settings have been modified, false otherwise
   */
  public boolean isModified(XStructureSettings data)
  {
    if (cbSyncMappings.isSelected() != data.isSyncMappingsAtStartup())
    {
      return true;
    }
    if (tfMappingsStorageDir.getText() != null ?
      !tfMappingsStorageDir.getText().equals(data.getMappingsStorageDir()) :
      data.getMappingsStorageDir() != null)
    {
      return true;
    }
    if (cbOverwriteWhenSyncMappings.isSelected() != data.isOverwriteWhenSyncMappings())
    {
      return true;
    }
    return false;
  }

  // To use a FileTextField
  private void createUIComponents()
  {
    FileChooserDescriptor fileChooserDescriptor = new FileChooserDescriptor(false, true, false,
      false, false, false);
    tfMappingsStorageDir =
      FileChooserFactory.getInstance().createFileTextField(fileChooserDescriptor,
        false, this).getField();
  }

  /**
   * {@inheritDoc}
   */
  public void dispose()
  {
  }

  /**
   * Method generated by IntelliJ IDEA GUI Designer
   * >>> IMPORTANT!! <<<
   * DO NOT edit this method OR call it in your code!
   *
   * @noinspection ALL
   */
  private void $$$setupUI$$$()
  {
    createUIComponents();
    contentPane = new JPanel();
    contentPane.setLayout(new GridLayoutManager(5, 3, new Insets(5, 5, 5, 5), -1, -1));
    cbSyncMappings = new JCheckBox();
    cbSyncMappings.setSelected(true);
    this.$$$loadButtonText$$$(cbSyncMappings,
      ResourceBundle.getBundle("org/sylfra/idea/plugins/xstructure/resources/XSBundle").getString(
        "settings.syncMappings.text"));
    cbSyncMappings.setToolTipText(
      ResourceBundle.getBundle("org/sylfra/idea/plugins/xstructure/resources/XSBundle").getString(
        "settings.syncMappings.description"));
    cbSyncMappings.putClientProperty("html.disable", Boolean.FALSE);
    cbSyncMappings.putClientProperty("hideActionText", Boolean.FALSE);
    contentPane.add(cbSyncMappings,
      new GridConstraints(2, 1, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_NONE,
        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED,
        null, new Dimension(220, 21), null, 0, false));
    final Spacer spacer1 = new Spacer();
    contentPane.add(spacer1,
      new GridConstraints(4, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1,
        GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
    final JPanel panel1 = new JPanel();
    panel1.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
    contentPane.add(panel1, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
      GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
      GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
    final JLabel label1 = new JLabel();
    this.$$$loadLabelText$$$(label1,
      ResourceBundle.getBundle("org/sylfra/idea/plugins/xstructure/resources/XSBundle").getString(
        "settings.mappingDir.text"));
    label1.setToolTipText(ResourceBundle.getBundle("org/sylfra/idea/plugins/xstructure/resources/XSBundle").getString(
      "settings.mappingDir.description"));
    panel1.add(label1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_NONE,
      GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(143, 13), null, 0,
      false));
    panel1.add(tfMappingsStorageDir,
      new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_NORTH, GridConstraints.FILL_HORIZONTAL,
        GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(300, 13), null, 0,
        false));
    cbOverwriteWhenSyncMappings = new JCheckBox();
    this.$$$loadButtonText$$$(cbOverwriteWhenSyncMappings,
      ResourceBundle.getBundle("org/sylfra/idea/plugins/xstructure/resources/XSBundle").getString(
        "settings.overwriteMappings.text"));
    cbOverwriteWhenSyncMappings.setToolTipText(
      ResourceBundle.getBundle("org/sylfra/idea/plugins/xstructure/resources/XSBundle").getString(
        "settings.overwriteMappings.description"));
    cbOverwriteWhenSyncMappings.putClientProperty("html.disable", Boolean.FALSE);
    cbOverwriteWhenSyncMappings.putClientProperty("hideActionText", Boolean.FALSE);
    contentPane.add(cbOverwriteWhenSyncMappings,
      new GridConstraints(3, 1, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_NONE,
        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED,
        null, new Dimension(220, 12), null, 2, false));
    label1.setLabelFor(tfMappingsStorageDir);
  }

  /**
   * @noinspection ALL
   */
  private void $$$loadLabelText$$$(JLabel component, String text)
  {
    StringBuffer result = new StringBuffer();
    boolean haveMnemonic = false;
    char mnemonic = '\0';
    int mnemonicIndex = -1;
    for (int i = 0; i < text.length(); i++)
    {
      if (text.charAt(i) == '&')
      {
        i++;
        if (i == text.length())
        {
          break;
        }
        if (!haveMnemonic && text.charAt(i) != '&')
        {
          haveMnemonic = true;
          mnemonic = text.charAt(i);
          mnemonicIndex = result.length();
        }
      }
      result.append(text.charAt(i));
    }
    component.setText(result.toString());
    if (haveMnemonic)
    {
      component.setDisplayedMnemonic(mnemonic);
      component.setDisplayedMnemonicIndex(mnemonicIndex);
    }
  }

  /**
   * @noinspection ALL
   */
  private void $$$loadButtonText$$$(AbstractButton component, String text)
  {
    StringBuffer result = new StringBuffer();
    boolean haveMnemonic = false;
    char mnemonic = '\0';
    int mnemonicIndex = -1;
    for (int i = 0; i < text.length(); i++)
    {
      if (text.charAt(i) == '&')
      {
        i++;
        if (i == text.length())
        {
          break;
        }
        if (!haveMnemonic && text.charAt(i) != '&')
        {
          haveMnemonic = true;
          mnemonic = text.charAt(i);
          mnemonicIndex = result.length();
        }
      }
      result.append(text.charAt(i));
    }
    component.setText(result.toString());
    if (haveMnemonic)
    {
      component.setMnemonic(mnemonic);
      component.setDisplayedMnemonicIndex(mnemonicIndex);
    }
  }

  /**
   * @noinspection ALL
   */
  public JComponent $$$getRootComponent$$$()
  {
    return contentPane;
  }
}
